import numpy as np
def simulateData(k, n):
X = np.row_stack([np.random.multivariate_normal(np.random.uniform(-10,10,2), 3*np.eye(2), size=n) for i in range(k)])
return X, np.repeat([str(i) for i in range(1,k+1)], n)TP4 - Clustering
Exploratory Data Analysis & Unsuperivsed Learning
Course: PHAUK Sokkey, PhD
TP: HAS Sothea, PhD
Objective: Clustering is technique of ML and Data Analysis used to group similar data points together. The goal is to partition a dataset into distinct subsets, or clusters, such that data points within each cluster are more similar to each other than to those in other clusters. This practical class aims to enhance your understanding of two different clustering algorithms, including their strengths and weaknesses.
The
Jupyter Notebookfor this TP can be downloaded here: TP4-Clustering.
1. Kmeans Algorithm
We will begin with a toy example using simulated dataset.
a. Write a function simulateData(k, n) that generates an ideal dataset for Kmeans, consisting of \(k\) groups with \(n\) observations within each group, of 2D normally distributed data points (you can choose any value of \(K\in\{3,4,5,...,8\}\)). Visualize your dataset, make sure that the groups are spread evenly. I set an example below:
# Simulate
data, labels = simulateData(3,100)
import pandas as pd
df_sim = pd.DataFrame({
'x' : data[:,0],
'y' : data[:,1],
'label' : labels
})import plotly.io as pio
pio.renderers.default = 'notebook'
import plotly.express as px
fig = px.scatter(df_sim, x="x", y="y", color="label")
fig.update_layout(width=500, height=400, title="Simulated data with 3 clusters")
fig.show()b. Perform Kmeans clustering algorithm on that the generated dataset using different number of clusters. - Check that the equality: With-class variance + Between-class variance = Total Variance. - For each number of cluster \(k\), compute within-class variances. - Draw the values of within-class variances as a function of number of cluster. - What do you observe?
from sklearn.cluster import KMeans
# To doc. Can you propose a systematic approach to approximate the most suitable number of clusters?
# To dod. Compute and visualize Silhouette Coefficient for each number of clusters considered above. Conclude.
from sklearn.metrics import silhouette_score
# To do2. Hirachical clustering
Unlike Kmeans algrithm, Hirachical clustering or hcluster does not require a prior number of clusters. It iteratively merges (agglomerative or bottom up approach) into less and less clusters starting from each points being a cluster on its own, or separate the data point (divisive or top-down approach) to create more and more clusters starting from one clsuter containing all data points.
a. Apply Hirachical cluster on the previously simulated dataset.
# To dob. Plot the associated Dendrograms of the resulting groups.
# To doc. Can you decide the most suitable number of clusters from the previous dendrogram?
# To do3. Real dataset
Now apply both algorithms on Iris dataset imported as follow:
from sklearn.datasets import load_iris
# Load data
Iris = load_iris()
X = Iris.data
y = Iris.target# To do